function setState(state) {
    gameArea.state = state;
}

function newGame() {
    gameArea.state = "game";
    gameView.state = "";
    gameView.hole1.state = "";
    gameView.hole2.state = "";
    gameView.hole3.state = "";
    gameView.hole4.state = "";
    gameView.hole5.state = "";
    gameView.hole6.state = "";
    gameView.hole7.state = "";
    gameView.hole8.state = "";
    gameView.hole1.image2 = "hole";
    gameView.hole2.image2 = "hole";
    gameView.hole3.image2 = "hole";
    gameView.hole4.image2 = "hole";
    gameView.hole5.image2 = "hole";
    gameView.hole6.image2 = "hole";
    gameView.hole7.image2 = "hole";
    gameView.hole8.image2 = "hole";

    gameView.hole1.hasMonkey = false;
    gameView.hole2.hasMonkey = false;
    gameView.hole3.hasMonkey = false;
    gameView.hole4.hasMonkey = false;
    gameView.hole5.hasMonkey = false;
    gameView.hole6.hasMonkey = false;
    gameView.hole7.hasMonkey = false;
    gameView.hole8.hasMonkey = false;

    clickCount = 0;
    gameArea.gameTimerInterval = 1000;
    gameArea.hideTimerInterval = 1000;
    gameTimer.interval = gameTimerInterval;
    gameView.hole1.flipSpeed = gameArea.gameTimerInterval / 2;
    gameView.hole2.flipSpeed = gameArea.gameTimerInterval / 2;
    gameView.hole3.flipSpeed = gameArea.gameTimerInterval / 2;
    gameView.hole4.flipSpeed = gameArea.gameTimerInterval / 2;
    gameView.hole5.flipSpeed = gameArea.gameTimerInterval / 2;
    gameView.hole6.flipSpeed = gameArea.gameTimerInterval / 2;
    gameView.hole7.flipSpeed = gameArea.gameTimerInterval / 2;
    gameView.hole8.flipSpeed = gameArea.gameTimerInterval / 2;
    gameTimer.restart();
    score = 0;
    lives = 3;

    gameView.scoreText = score;
    gameView.livesText = lives;
    gameView.gamesPlayed = gameView.gamesPlayed + 1;
}

var blocks = []
function getSize() {
   var size = 0;
   blocks = [];
    if (gameView.hole1.state != "hasMonkey") {
        size++;
        blocks.push(1);
    }
    if (gameView.hole2.state != "hasMonkey"){
        size++;
        blocks.push(2);
    }
    if (gameView.hole3.state != "hasMonkey"){
        size++;
        blocks.push(3);
    }
    if (gameView.hole4.state != "hasMonkey"){
        size++;
        blocks.push(4);
    }
    if (gameView.hole5.state != "hasMonkey") {
        size++;
        blocks.push(5);
    }
    if (gameView.hole6.state != "hasMonkey") {
        size++;
        blocks.push(6);
    }
    if (gameView.hole7.state != "hasMonkey") {
        size++;
        blocks.push(7);
    }
    if (gameView.hole8.state != "hasMonkey") {
        size++;
        blocks.push(8);
    }

    return size;
}

function gameLoop(loop)
{
    var size = getSize();

    if (size == 0) {
        gameTimer.stop();
        endGame();

        gameTimer.stop();
    }
    else {
       var randomnumber = Math.floor(Math.random() * size);
       var randomnumber2 = Math.floor(Math.random() * 101);

       var block = blocks[randomnumber];

       if (randomnumber2 < 50) {
           setMonkey(block, "holebg1");
       }
       else if (randomnumber2 >= 50 && randomnumber2 < 80) {
           setMonkey(block, "holebg2");
       }
       else if (randomnumber2 >= 80) {
            setMonkey(block, "holebg3");
       }

        if (randomnumber2 % 7 === 0 && loop) {
            gameLoop(false);
        }

    }
}


function setMonkey(id, image) {

    if (id == 1) {
        gameView.hole1.state = "hasMonkey";
        gameView.hole1.hasMonkey = true;
        gameView.hole1.image = image;
        hideTimer1.restart();
    }
    else if (id == 2) {
        gameView.hole2.state = "hasMonkey";
        gameView.hole2.hasMonkey = true;
        gameView.hole2.image = image;
        hideTimer2.restart();
    }
    else if (id == 3) {
        gameView.hole3.state = "hasMonkey";
        gameView.hole3.hasMonkey = true;
        gameView.hole3.image = image;
        hideTimer3.restart();
    }
    else if (id == 4) {
        gameView.hole4.state = "hasMonkey";
        gameView.hole4.hasMonkey = true;
        gameView.hole4.image = image;
        hideTimer4.restart();
    }
    else if (id == 5) {
        gameView.hole5.state = "hasMonkey";
        gameView.hole5.hasMonkey = true;
        gameView.hole5.image = image;
        hideTimer5.restart();
    }
    else if (id == 6) {
        gameView.hole6.state = "hasMonkey";
        gameView.hole6.hasMonkey = true;
        gameView.hole6.image = image;
        hideTimer6.restart();
    }
    else if (id == 7) {
        gameView.hole7.state = "hasMonkey";
        gameView.hole7.hasMonkey = true;
        gameView.hole7.image = image;
        hideTimer7.restart();
    }
    else if (id == 8) {
        gameView.hole8.state = "hasMonkey";
        gameView.hole8.hasMonkey = true;
        gameView.hole8.image = image;
        hideTimer8.restart();
    }
}

function hideMonkey(id) {

    if (id == 1 && gameView.hole1.hasMonkey) {
        gameView.hole1.state = "";
        gameView.hole1.image2 = "holebgred";
        gameView.hole1.hasMonkey = false;
        hideTimer1.stop();
        gameArea.lives--;

    }
    else if (id == 2 && gameView.hole2.hasMonkey) {
        gameView.hole2.state = "";
        gameView.hole2.image2 = "holebgred";
        gameView.hole2.hasMonkey = false;
        hideTimer2.stop();
        gameArea.lives--;

    }
    else if (id == 3 && gameView.hole3.hasMonkey) {
        gameView.hole3.state = "";
        gameView.hole3.image2 = "holebgred";
        gameView.hole3.hasMonkey = false;
        hideTimer3.stop();
        gameArea.lives--;

    }
    else if (id == 4 && gameView.hole4.hasMonkey) {
        gameView.hole4.state = "";
        gameView.hole4.image2 = "holebgred";
        gameView.hole4.hasMonkey = false;
        hideTimer4.stop();
        gameArea.lives--;

    }
    else if (id == 5 && gameView.hole5.hasMonkey) {
        gameView.hole5.state = "";
        gameView.hole5.image2 = "holebgred";
        gameView.hole5.hasMonkey = false;
        hideTimer5.stop();
        gameArea.lives--;

    }
    else if (id == 6 && gameView.hole6.hasMonkey) {
        gameView.hole6.state = "";
        gameView.hole6.image2 = "holebgred";
        gameView.hole6.hasMonkey = false;
        hideTimer6.stop();
        gameArea.lives--;
    }
    else if (id == 7 && gameView.hole7.hasMonkey) {
        gameView.hole7.state = "";
        gameView.hole7.image2 = "holebgred";
        gameView.hole7.hasMonkey = false;
        hideTimer7.stop();
        gameArea.lives--;
    }
    else if (id == 8 && gameView.hole8.hasMonkey) {
        gameView.hole8.state = "";
        gameView.hole8.image2 = "holebgred";
        gameView.hole8.hasMonkey = false;
        hideTimer8.stop();
        gameArea.lives--;
    }

    gameView.livesText = gameArea.lives;

    if (gameArea.lives == 0) {

        endGame();

        gameTimer.stop();
        hideTimer1.stop();
        hideTimer2.stop();
        hideTimer3.stop();
        hideTimer4.stop();
        hideTimer5.stop();
        hideTimer6.stop();
        hideTimer7.stop();
        hideTimer8.stop();

    }
}

function addScore(score,id) {

    if (id == 1) {
        hideTimer1.stop();
        gameView.hole1.hasMonkey = false;
        gameView.hole1.state = "";
        gameView.hole1.image2 = "hole";
    }
    else if (id == 2) {
        hideTimer2.stop();
        gameView.hole2.hasMonkey = false;
        gameView.hole2.state = "";
        gameView.hole2.image2 = "hole";
    }
    else if (id == 3) {
        hideTimer3.stop();
        gameView.hole3.hasMonkey = false;
        gameView.hole3.state = "";
        gameView.hole3.image2 = "hole";
    }
    else if (id == 4) {
        hideTimer4.stop();
        gameView.hole4.hasMonkey = false;
        gameView.hole4.state = "";
        gameView.hole4.image2 = "hole";
    }
    else if (id == 5) {
        hideTimer5.stop();
        gameView.hole5.hasMonkey = false;
        gameView.hole5.state = "";
        gameView.hole5.image2 = "hole";
    }
    else if (id == 6) {
        hideTimer6.stop();
        gameView.hole6.hasMonkey = false;
        gameView.hole6.state = "";
        gameView.hole6.image2 = "hole";
    }
    else if (id == 7) {
        hideTimer7.stop();
        gameView.hole7.hasMonkey = false;
        gameView.hole7.state = "";
        gameView.hole7.image2 = "hole";
    }
    else if (id == 8) {
        hideTimer8.stop();
        gameView.hole8.hasMonkey = false;
        gameView.hole8.state = "";
        gameView.hole8.image2 = "hole";
    }

    if (gameView.state != "")
        return;

    gameArea.score += score;
    gameView.scoreText = gameArea.score;
    clickCount++;

    if (gameArea.gameTimerInterval > 250 && clickCount % 7 == 0) {
        gameArea.gameTimerInterval = gameArea.gameTimerInterval -50;
        gameTimer.interval = gameArea.gameTimerInterval;

        gameView.hole1.flipSpeed = gameArea.gameTimerInterval / 2;
        gameView.hole2.flipSpeed = gameArea.gameTimerInterval / 2;
        gameView.hole3.flipSpeed = gameArea.gameTimerInterval / 2;
        gameView.hole4.flipSpeed = gameArea.gameTimerInterval / 2;
        gameView.hole5.flipSpeed = gameArea.gameTimerInterval / 2;
        gameView.hole6.flipSpeed = gameArea.gameTimerInterval / 2;
        gameView.hole7.flipSpeed = gameArea.gameTimerInterval / 2;
        gameView.hole8.flipSpeed = gameArea.gameTimerInterval / 2;
    }
}

function saveHighScore() {
     //if (scoresURL != "")
        //sendHighScore(name);

     var db = openDatabaseSync("HitTheMonkeyScores", "1.0", "Local HitTheMonkey High Scores", 100);
     var dataStr = "INSERT INTO Scores VALUES(?, ?, ?)";
     var data = ["", score, 8];

     db.transaction(function(tx) {
         tx.executeSql('CREATE TABLE IF NOT EXISTS Scores(name TEXT, score NUMBER, gridSize NUMBER)');
         tx.executeSql(dataStr, data);
     });

    //getScores();
    //setState("highscore");
 }

function endGame() {
    if (score == 0) {
        gameView.state = "gameOver1";
        return;
    }

    var db = openDatabaseSync("HitTheMonkeyScores", "1.0", "Local HitTheMonkey High Scores", 100);
    db.transaction(function(tx) {
     tx.executeSql('CREATE TABLE IF NOT EXISTS Scores(name TEXT, score NUMBER, gridSize NUMBER)');
     var rs = tx.executeSql('SELECT * FROM Scores WHERE gridSize = 8 AND score >= ' + score + ' LIMIT 10');

     if (rs.rows.length == 10) {
         gameView.state = "gameOver1";
     }
     else {
         gameView.state = "gameOver";
     }

    });
}

function getScores() {
   var db = openDatabaseSync("HitTheMonkeyScores", "1.0", "Local HitTheMonkey High Scores", 100);

   db.transaction(function(tx) {
    tx.executeSql('CREATE TABLE IF NOT EXISTS Scores(name TEXT, score NUMBER, gridSize NUMBER)');

    var rs = tx.executeSql('SELECT * FROM Scores WHERE gridSize = 8 ORDER BY score desc LIMIT 10');
    var r = ""
    for (var i = 0; i < rs.rows.length; i++) {
        r += (i + 1) + ". " + rs.rows.item(i).score + ' points.\n';
    }
    highScoreView.highscores = r;
   });
}

